/*
 * Created by JFormDesigner on Tue May 13 23:27:27 CST 2025
 */

package com.soft.bams.server;

import com.soft.bams.service.Bank;
import com.soft.bams.vo.RankVO;

import java.awt.*;
import java.awt.event.*;
import java.util.TreeSet;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 23838
 */
public class RankPanel extends JPanel {
//    Bank bank = Bank.getInstance();
//
//    public RankPanel() {
//        initComponents();
//    }
//
//    //点击按钮查看总余额
//    private void totalBalance(ActionEvent e) {
//        double balance = bank.totalBalance();
//        moneylabel.setText(balance+"元");
//    }
//
//    //点击按钮查看总透支额度
//    private void totalCeiling(ActionEvent e) {
//        double ceiling = bank.totalCeiling();
//        moneylabel.setText(ceiling+"元");
//    }
//
//    //点击按钮查看总贷款额度
//
//    private void totalLoan(ActionEvent e) {
//        double loanAmount = bank.totalLoan();
//        moneylabel.setText(loanAmount+"元");
//    }
//
//    //点击按钮查看排行榜
//    private void rank(ActionEvent e) {
//        TreeSet<RankVO> rank = bank.rank();
//
//        //表格标题
//        String[] titles = {"序号" , "身份证号" , "姓名" , "总资产"};
//
//        //表格数据
//        Object[][] data = new Object[rank.size()][titles.length];
//        //序号
//        int index = 0;
//        for (RankVO rankVO : rank) {
//            data[index] = new Object[]{
//                    ++index,
//                    rankVO.getPersonId(),
//                    rankVO.getName(),
//                    rankVO.getTotal()
//            };
//        }
//        //表格中填充数据
//        DefaultTableModel tableModel = new DefaultTableModel(data , titles);
//        JTable table = new JTable(tableModel);
//        //将表格添加至scrollPane中
//        scrollPane.setViewportView(table);
//    }
//
//    //点击返回，可以跳转至服务界面
//    private void back(ActionEvent e) {
//        CardLayout card = (CardLayout)this.getParent().getLayout();
//        card.show(this.getParent() , "server");
//
//        //清空数据
//        moneylabel.setText("0.0元");
//        moneylabel.setHorizontalAlignment(SwingConstants.CENTER);
//        scrollPane.setViewportView(moneylabel);
//    }
    Bank bank = Bank.getInstance();
    public RankPanel() {
        initComponents();
    }

    private void totalBalance(ActionEvent e) {
        double balance = bank.totalBalance();
        moneylabel.setText(balance + "元");
        scrollPane.setViewportView(moneylabel); // 确保切换回显示余额
    }

    private void totalCeiling(ActionEvent e) {
        double ceiling = bank.totalCeiling();
        moneylabel.setText(ceiling + "元");
        scrollPane.setViewportView(moneylabel);
    }

    private void totalLoan(ActionEvent e) {
        double loanAmount = bank.totalLoan();
        moneylabel.setText(loanAmount + "元");
        scrollPane.setViewportView(moneylabel);
    }

    private void rank(ActionEvent e) {
        TreeSet<RankVO> rank = bank.rank();
        String[] titles = {"序号", "身份证号", "姓名", "总资产"};
        Object[][] data = new Object[rank.size()][titles.length];
        int index = 0;
        for (RankVO rankVO : rank) {
            data[index] = new Object[]{
                    ++index,
                    rankVO.getPersonId(),
                    rankVO.getName(),
                    rankVO.getTotal()
            };
        }
        DefaultTableModel tableModel = new DefaultTableModel(data, titles);
        JTable table = new JTable(tableModel);
        scrollPane.setViewportView(table); // 显示排行榜表格
    }

    private void back(ActionEvent e) {
        CardLayout card = (CardLayout) this.getParent().getLayout();
        card.show(this.getParent(), "server");
        moneylabel.setText("0.0元");
        moneylabel.setHorizontalAlignment(SwingConstants.CENTER);
        scrollPane.setViewportView(moneylabel); // 确保切换回显示余额
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        totalBalancebutton = new JButton();
        totalCeilingbutton = new JButton();
        totalLoanbutton = new JButton();
        rankbutton = new JButton();
        backbutton = new JButton();
        scrollPane = new JScrollPane();
        moneylabel = new JLabel();

        //======== this ========
        setLayout(null);

        //---- totalBalancebutton ----
        totalBalancebutton.setText("\u67e5\u770b\u603b\u4f59\u989d");
        totalBalancebutton.addActionListener(e -> totalBalance(e));
        add(totalBalancebutton);
        totalBalancebutton.setBounds(30, 70, 120, 34);

        //---- totalCeilingbutton ----
        totalCeilingbutton.setText("\u67e5\u770b\u900f\u652f\u989d\u5ea6");
        totalCeilingbutton.addActionListener(e -> totalCeiling(e));
        add(totalCeilingbutton);
        totalCeilingbutton.setBounds(30, 135, 120, 34);

        //---- totalLoanbutton ----
        totalLoanbutton.setText("\u67e5\u770b\u8d37\u6b3e\u603b\u989d");
        totalLoanbutton.addActionListener(e -> totalLoan(e));
        add(totalLoanbutton);
        totalLoanbutton.setBounds(30, 200, 120, 34);

        //---- rankbutton ----
        rankbutton.setText("\u67e5\u770b\u6392\u884c\u699c");
        rankbutton.addActionListener(e -> rank(e));
        add(rankbutton);
        rankbutton.setBounds(30, 265, 120, 34);

        //---- backbutton ----
        backbutton.setText("\u8fd4\u56de");
        backbutton.addActionListener(e -> back(e));
        add(backbutton);
        backbutton.setBounds(30, 330, 120, 34);

        //======== scrollPane ========
        {

            //---- moneylabel ----
            moneylabel.setText("0.0\u5143");
            moneylabel.setHorizontalAlignment(SwingConstants.CENTER);
            scrollPane.setViewportView(moneylabel);
        }
        add(scrollPane);
        scrollPane.setBounds(160, 70, 225, 290);

        setPreferredSize(new Dimension(400, 455));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton totalBalancebutton;
    private JButton totalCeilingbutton;
    private JButton totalLoanbutton;
    private JButton rankbutton;
    private JButton backbutton;
    private JScrollPane scrollPane;
    private JLabel moneylabel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
